// SpringFunction.cpp: implementation of the SpringFunction class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SpringFunction.h"
#include "SpringSelRegular.h"
#include "SpringSelRegularGenerator.h"
#include "SupportHanger.h"
#include "PhscaculateSetting.h"
#include "PersistentFacade.h"
#include "PartCrudeDataSet.h"

#define UNIT_CHANGE(a) a/9.8
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC_CREATE(SpringFunction, FunctionObject, FunctionObject::eSELSPRING)

SpringFunction::SpringFunction()
{
	m_pSpringPartDH = NULL;
}

SpringFunction::~SpringFunction()
{

}

BOOL SpringFunction::Execute()
{
	SpringSelRegular* pRegular = GetSpringSelRegularGenerator().Generator();
	pRegular->SetOwner(static_cast<Part*>(GetOwner()));
	SetRegularParam(pRegular);
	Spring* pSpring = pRegular->GetSpring();
	if(pSpring == NULL)
		return FALSE;
	SelSpringPart(pSpring);
	delete pSpring;
	return TRUE;
}
/*
void SpringFunction::RegisterToSupport()
{
}*/
/*
void SpringFunction::RegisterToString()
{
	Part* pPart = GetOwner();
	ASSERT(pPart);
	PartString* pString = pPart->GetOwner();
	ASSERT(pString);

	FunctionObject* pFunction = NULL;
	SpringFunction* pSpring = NULL;
	pFunction = pString->GetFunction(this->GetFunctionStyle());
	if( pFunction != NULL)
	{
		ASSERT(pFunction->GetRuntimeClass() == RUNTIME_CLASS_EX(SpringFunction));
		pSpring = static_cast<SpringFunction*>(pFunction);
		pSpring->m_pNextSpring = this;
		return;
	}

	pString->AttachFunction(this);
}*/
void SpringFunction::SetRegularParam(SpringSelRegular* pRegular)
{
/*	ASSERT(GetOwner());
	ASSERT(GetOwner()->GetOwner());

	PartString* pString = GetOwner()->GetOwner();
	if(GetCurCaculateSetting().IsZeroLoadOfHot())
	{
		pRegular->WorkLoad(UNIT_CHANGE(pString->WorkLoad().length()));
		pRegular->HotDisplacement(pString->HotDisplacement());
	}
	else
	{
		pRegular->WorkLoad(UNIT_CHANGE(pString->AssembleLoad().length()));
		pRegular->HotDisplacement(-pString->HotDisplacement());
	}
	pRegular->LoadChangeCoefficient(
		GetCurCaculateSetting().GetSpringLoadChangeCoefficient());*/
}

INT SpringFunction::GetSpringNum()
{
/*	Part* pPart = GetOwner();
	ASSERT(pPart);
	PartString* pString = pPart->GetOwner();
	ASSERT(pString);

	INT nCount = 0;
	SpringFunction* pSpring = NULL;
	FunctionObject* pFunction = NULL;
	pFunction = pString->GetFunction(this->GetFunctionStyle());
	while(pFunction != NULL)
	{
		ASSERT(pFunction->GetRuntimeClass() == RUNTIME_CLASS_EX(SpringFunction));
		pSpring = static_cast<SpringFunction*>(pFunction);
		nCount++;
		pFunction = pSpring->NextRegister();
	}
	return nCount;*/
	return 0;
}

BOOL SpringFunction::SelSpringPart(Spring* pSpring)
{
	ASSERT(pSpring);

	return FirstPartPriorityStratigy(pSpring);

}

BOOL SpringFunction::FirstPartPriorityStratigy(Spring* pSpring)
{
/*	INT nSpringNum = GetSpringNum();

	if(nSpringNum > pSpring->SpringSerialNum())
		return FALSE;

	SpringFunction* pCurFunction = NULL;
	INT nFirstPartSerialNum = 0;
	INT SerialNum = pSpring->SpringSerialNum();
	pCurFunction = this;
	nSpringNum--;
	while(pCurFunction != NULL)
	{
		nFirstPartSerialNum = SerialNum - nSpringNum;
		
		CString strRegular;
		strRegular.Format(_T("(%s mod 100)= %d AND int(%s/100)<= %d ORDER BY %s DESC"),
			pCurFunction->m_pSpringPartDH->GetSymbol(), pSpring->Number(), 
			pCurFunction->m_pSpringPartDH->GetSymbol(), nFirstPartSerialNum,
			pCurFunction->m_pSpringPartDH->GetSymbol());
		
		ASSERT(pCurFunction->GetOwner());
		PartCrudeDataSet* pSet = GetPersistentFacade().GetPartCrudeDataSet();
		pSet->SetDataStandard(pCurFunction->GetOwner()->GetStandard());
		if(!pSet->InstancePart(pCurFunction->GetOwner(),
			pCurFunction->GetOwner()->GetCustomID(), strRegular))
		{
			pSet->Close();
			return FALSE;
		}
		pSet->Close();

		SerialNum -= GetPartSpringNum(pCurFunction);
		nSpringNum--;
		pCurFunction = static_cast<SpringFunction*>(pCurFunction->NextRegister());		
	}*/
	return FALSE;
}

INT SpringFunction::GetPartSpringNum(SpringFunction* pFunction)
{
/*	ASSERT(pFunction);
	ASSERT(pFunction->GetOwner());
	ASSERT(m_pSpringPartDH);

	INT nDH = (INT)(*(pFunction->m_pSpringPartDH));
	return (INT)nDH/100;*/
	return 0;
}